package com.acwing.partition43;

import java.io.*;
import java.util.Arrays;

/**
 * @author rkc
 * @date 2022/7/28 9:35
 */
public class AC4291丛林之路 {

    private static final int N = 30;
    private static int[] parent = new int[N];
    private static Edge[] edges = new Edge[N * N];
    private static int n, size = 0;

    private static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    private static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        while (true) {
            n = nextInt();
            if (n == 0) break;
            size = 0;
            for (int i = 1; i <= n - 1; i++) {
                int u = nextChar() - 'A', k = nextInt();
                for (int j = 0; j < k; j++) {
                    int v = nextChar() - 'A', w = nextInt();
                    edges[size++] = new Edge(u, v, w);
                }
            }
            writer.println(kruskal());
        }
        writer.flush();
    }

    private static int kruskal() {
        for (int i = 0; i <= n; i++) parent[i] = i;
        Arrays.sort(edges, 0, size);
        int res = 0;
        for (int i = 0; i < size; i++) {
            int u = edges[i].u, v = edges[i].v, w = edges[i].w, fu = find(u), fv = find(v);
            if (fu != fv) {
                parent[fu] = fv;
                res += w;
            }
        }
        return res;
    }

    private static int find(int x) {
        if (x != parent[x]) parent[x] = find(parent[x]);
        return parent[x];
    }

    private static class Edge implements Comparable<Edge> {
        private int u, v, w;

        public Edge(int u, int v, int w) {
            this.u = u;
            this.v = v;
            this.w = w;
        }

        @Override
        public int compareTo(Edge o) {
            return Integer.compare(w, o.w);
        }
    }

    private static int nextInt() throws IOException {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }

    private static char nextChar() throws IOException {
        tokenizer.nextToken();
        return tokenizer.sval.charAt(0);
    }
}
